import numpy as np
import json
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def load_data():
    # 从文件导入数据
    datafile = './data/housing.data'
    data = np.fromfile(datafile, sep=' ')

    # 每条数据包括14项，其中前面13项是影响因素，第14项是相应的房屋价格中位数
    feature_names = [ 'CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', \
                      'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV' ]
    feature_num = len(feature_names)

    # 将原始数据进行Reshape，变成[N, 14]这样的形状
    data = data.reshape([data.shape[0] // feature_num, feature_num])

    # 将原数据集拆分成训练集和测试集
    # 这里使用80%的数据做训练，20%的数据做测试
    # 测试集和训练集必须是没有交集的
    ratio = 0.8
    offset = int(data.shape[0] * ratio)
    training_data = data[:offset]

    # 计算训练集的最大值，最小值，平均值
    maximums, minimums, avgs = training_data.max(axis=0), training_data.min(axis=0), \
                                 training_data.sum(axis=0) / training_data.shape[0]

    # 对数据进行归一化处理
    for i in range(feature_num):
        #print(maximums[i], minimums[i], avgs[i])
        data[:, i] = (data[:, i] - avgs[i]) / (maximums[i] - minimums[i])

    # 训练集和测试集的划分比例
    training_data = data[:offset]
    test_data = data[offset:]
    return training_data, test_data

class NetWork(object):
    def __init__(self,num_of_weights):
    # 随机产生w的初始值
    # 为了保持程序每次运行结果的一致性，
    # 此处设置固定的随机数种子
        np.random.seed(0)
        self.w = np.random.randn(num_of_weights,1)
        self.b = 0.

    def forward(self,x):
        z = np.dot(x,self.w)+self.b
        return z

    def loss(self,z,y):
        error = z - y
        cost = error * error
        cost = np.mean(cost)

        return cost

    def gradient(self,x,y):
        z = self.forward(x)
        gradient_w = (z-y)*x
        gradient_w = np.mean(gradient_w,axis=0)
        gradient_w = gradient_w[:,np.newaxis]
        gradient_b = (z-y)
        gradient_b = np.mean(gradient_b)

        return gradient_w,gradient_b

    def update(self,gradient_w,gradient_b,eta=0.01):
        self.w = self.w - eta * gradient_w
        self.b = self.b - eta * gradient_b

    def train(self, training_data, num_epochs, batch_size=10, eta=0.01):
        n = len(training_data)
        points = []
        losses = []
        for epoch_id in range(num_epochs):
            # 在每轮迭代开始之前，将训练数据的顺序随机打乱 //epoch代表所有的数据遍历几次
            # 然后再按每次取batch_size条数据的方式取出
            np.random.shuffle(training_data)
            # 将训练数据进行拆分，每个mini_batch包含batch_size条的数据
            mini_batches = [training_data[k:k+batch_size] for k in range(0, n, batch_size)]
            for iter_id, mini_batch in enumerate(mini_batches):
                #print(self.w.shape)
                #print(self.b)
                x = mini_batch[:, :-1]
                y = mini_batch[:, -1:]
                a = self.forward(x)
                loss = self.loss(a, y)
                gradient_w, gradient_b = self.gradient(x, y)
                self.update(gradient_w, gradient_b, eta)
                losses.append(loss)
                points.append([gradient_w,gradient_b])
                print('Epoch {:3d} / iter {:3d}, loss = {:.4f}'.format(epoch_id, iter_id, loss))

        index = losses.index(min(losses))
        print("loss最小为:"+str(min(losses)))
        print("best w is "+str(points[index][0])+" b is "+str(points[index][1]))

        return losses


def main():
    training_data, test_data = load_data()
    print(training_data)
    x = training_data[:, :-1]
    y = training_data[:, -1:]

    w = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, -0.1, -0.2, -0.3, -0.4, 0.0]
    w = np.array(w).reshape([13, 1])

    net = NetWork(13)

    x1 = x[0]

    z = net.forward(x1)
    # print("z="+str(z))
    #
    # x1 = x[0:3]
    # y1 = y[0:3]
    # z = net.forward(x1)
    # print("predict:\n",z)
    #
    # loss  = net.loss(z,y1)
    # print('loss:',loss)
    #
    # losses = []
    #只画出参数w5和w9在区间[-160, 160]的曲线部分，以及包含损失函数的极值
    # w5 = np.arange(-160.0, 160.0, 1.0)
    # w9 = np.arange(-160.0, 160.0, 1.0)
    # losses = np.zeros([len(w5), len(w9)])

    #计算设定区域内每个参数取值所对应的Loss
    # for i in range(len(w5)):
    #     for j in range(len(w9)):
    #         net.w[5] = w5[i]
    #         net.w[9] = w9[j]
    #         z = net.forward(x)
    #         loss = net.loss(z, y)
    #         losses[i, j] = loss

    #使用matplotlib将两个变量和对应的Loss作3D图
    # import matplotlib.pyplot as plt
    # from mpl_toolkits.mplot3d import Axes3D
    # fig = plt.figure()
    # ax = Axes3D(fig)
    #
    # w5, w9 = np.meshgrid(w5, w9)
    #
    # ax.plot_surface(w5, w9, losses, rstride=1, cstride=1, cmap='rainbow')
    # plt.show()


    #梯度计算
    # 注意这里是一次取出3个样本的数据，不是取出第3个样本
    # z = net.forward(x)
    # gradient_w = (z - y) * x
    # print('gradient_w shape {}'.format(gradient_w.shape))
    # print(gradient_w)
    #
    # # axis = 0 表示把每一行做相加然后再除以总的行数
    # gradient_w = np.mean(gradient_w, axis=0)
    # print('gradient_w ', gradient_w.shape)
    # print('w ', net.w.shape)
    # print(gradient_w)
    # print(net.w)
    #
    # gradient_w = gradient_w[:, np.newaxis]
    # print('gradient_w shape', gradient_w.shape)
    #
    # gradient_b = (z - y)
    # gradient_b = np.mean(gradient_b)
    # # 此处b是一个数值，所以可以直接用np.mean得到一个标量
    # print(gradient_b)

    # 调用上面定义的gradient函数，计算梯度
    # 初始化网络
    # Step
    # net = NetWork(13)
    # # 设置[w5, w9] = [-100., -100.]
    # net.w[5] = -100.0
    # net.w[9] = -100.0
    #
    # z = net.forward(x)
    # loss = net.loss(z, y)
    # gradient_w, gradient_b = net.gradient(x, y)
    # gradient_w5 = gradient_w[5][0]
    # gradient_w9 = gradient_w[9][0]
    # print('point {}, loss {}'.format([net.w[5][0], net.w[9][0]], loss))
    # print('gradient {}'.format([gradient_w5, gradient_w9]))
    #
    # # 在[w5, w9]平面上，沿着梯度的反方向移动到下一个点P1
    # # 定义移动步长 eta
    #Step
    # eta = 0.1
    # # 更新参数w5和w9
    # net.w[5] = net.w[5] - eta * gradient_w5
    # net.w[9] = net.w[9] - eta * gradient_w9
    # # 重新计算z和loss
    # z = net.forward(x)
    # loss = net.loss(z, y)
    # gradient_w, gradient_b = net.gradient(x, y)
    # gradient_w5 = gradient_w[5][0]
    # gradient_w9 = gradient_w[9][0]
    #
    #
    # print('point {}, loss {}'.format([net.w[5][0], net.w[9][0]], loss))
    # print('gradient {}'.format([gradient_w5, gradient_w9]))

    #Step
    # 获取数据
    # train_data, test_data = load_data()
    # x = train_data[:, :-1]
    # y = train_data[:, -1:]
    # # 创建网络
    # net = NetWork(13)
    # num_iterations=1000
    # # 启动训练
    # losses = net.train(x, y, iterations=num_iterations, eta=0.03)
    #
    # # 画出损失函数的变化趋势
    # plot_x = np.arange(num_iterations)
    # plot_y = np.array(losses)
    # plt.plot(plot_x, plot_y)
    # plt.show()

    #Step
    # Step
    # train_data,test_data = load_data()
    # batch_size = 10
    # n = len(train_data)
    # mini_batches = [train_data[k:k+batch_size] for k in range(0, n, batch_size)]
    # print('total number of mini_batches is ', len(mini_batches))
    # print('first mini_batch shape ', mini_batches[0].shape)
    # print('last mini_batch shape ', mini_batches[-1].shape)

    losses = net.train(training_data, num_epochs=50, batch_size=100, eta=0.1)

    # 画出损失函数的变化趋势
    plot_x = np.arange(len(losses))
    plot_y = np.array(losses)
    plt.plot(plot_x, plot_y)
    plt.show()

    print(np.min(losses))

if __name__ == "__main__":
    main()

    #net = NetWork(13)
